<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../Universal.css">
</head>
<body>
    
    <div id="cnblogs_post_body" class="blogpost-body">
        <h2>Properties类</h2>
    <p>Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。</p>
    <p>特点：</p>
    <ul>
    <li>Hashtable的子类，map集合中的方法都可以用。</li>
    <li>该集合没有泛型。键值都是字符串。</li>
    <li>它是一个可以持久化的属性集。键值可以存储到集合中，也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。</li>
    <li>有和流技术相结合的方法。</li>
    </ul>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 0, 1)">load(InputStream)  把指定流所对应的文件中的数据，读取出来，保存到Propertie集合中
    load(Reader)  
    store(OutputStream,commonts)把集合中的数据，保存到指定的流所对应的文件中，参数commonts代表对描述信息
    stroe(Writer,comments);</span></pre>
    </div>
    <p>代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> io;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Properties;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Set;
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
     *
     * Properties集合，它是唯一一个能与IO流交互的集合
     *
     * 需求：向Properties集合中添加元素，并遍历
     *
     * 方法：
     * public Object setProperty(String key, String value)调用 Hashtable 的方法 put。
     * public Set&lt;String&gt; stringPropertyNames()返回此属性列表中的键集，
     * public String getProperty(String key)用指定的键在此属性列表中搜索属性
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> PropertiesDemo01 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建集合对象</span>
            Properties prop = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Properties();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加元素到集合
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">prop.put(key, value);</span>
            prop.setProperty("周迅", "张学友"<span style="color: rgba(0, 0, 0, 1)">);
            prop.setProperty(</span>"李小璐", "贾乃亮"<span style="color: rgba(0, 0, 0, 1)">);
            prop.setProperty(</span>"杨幂", "刘恺威"<span style="color: rgba(0, 0, 0, 1)">);
    
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println(prop);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">测试的使用
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">遍历集合</span>
            Set&lt;String&gt; keys =<span style="color: rgba(0, 0, 0, 1)"> prop.stringPropertyNames();
            </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String key : keys) {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过键 找值
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">prop.get(key)</span>
                String value =<span style="color: rgba(0, 0, 0, 1)"> prop.getProperty(key);
                System.out.println(key</span>+"==" +<span style="color: rgba(0, 0, 0, 1)">value);
            }
        }
    }</span></pre>
    </div>
    <h3>将集合中内容存储到文件&nbsp;</h3>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> io;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.FileWriter;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Properties;
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">需求：使用Properties集合，完成把集合内容存储到IO流所对应文件中的操作
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">分析：
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1，创建Properties集合
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2，添加元素到集合
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3，创建流
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4，把集合中的数据存储到流所对应的文件中
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">stroe(Writer,comments)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">store(OutputStream,commonts)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把集合中的数据，保存到指定的流所对应的文件中，参数commonts代表对描述信息
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5，关闭流
    </span><span style="color: rgba(0, 128, 0, 1)">//</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">代码演示：</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> PropertiesDemo02 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1，创建Properties集合</span>
            Properties prop = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Properties();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2，添加元素到集合</span>
            prop.setProperty("周迅", "张学友"<span style="color: rgba(0, 0, 0, 1)">);
            prop.setProperty(</span>"李小璐", "贾乃亮"<span style="color: rgba(0, 0, 0, 1)">);
            prop.setProperty(</span>"杨幂", "刘恺威"<span style="color: rgba(0, 0, 0, 1)">);
    
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3，创建流</span>
            FileWriter out = <span style="color: rgba(0, 0, 255, 1)">new</span> FileWriter("prop.properties"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4，把集合中的数据存储到流所对应的文件中</span>
            prop.store(out, "save data"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5，关闭流</span>
    <span style="color: rgba(0, 0, 0, 1)">        out.close();
        }
    }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170719172027521-1800348556.png" alt=""></p>
    <h3>读取文件中的数据，并保存到集合</h3>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> io;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.FileInputStream;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Properties;
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">需求：从属性集文件prop.properties 中取出数据，保存到集合中
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        分析：
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        1，创建集合
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        2，创建流对象
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        3,把流所对应文件中的数据 读取到集合中
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        load(InputStream)  把指定流所对应的文件中的数据，读取出来，保存到Propertie集合中
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        load(Reader)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        4,关闭流
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        5,显示集合中的数据
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        代码演示：</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> PropertiesDemo03 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1，创建集合</span>
            Properties prop = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Properties();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2，创建流对象</span>
            FileInputStream in = <span style="color: rgba(0, 0, 255, 1)">new</span> FileInputStream("prop.properties"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">FileReader in = new FileReader("prop.properties");
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3,把流所对应文件中的数据 读取到集合中</span>
    <span style="color: rgba(0, 0, 0, 1)">        prop.load(in);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4,关闭流</span>
    <span style="color: rgba(0, 0, 0, 1)">        in.close();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5,显示集合中的数据</span>
    <span style="color: rgba(0, 0, 0, 1)">        System.out.println(prop);
    
        }
    }</span></pre>
    </div>
    <p>注意：使用字符流FileReader就可以完成文件中的中文读取操作了。</p>
    <h2>序列化流与反序列化流&nbsp;</h2>
    <p>用于从流中读取对象的操作流 ObjectInputStream 称为 反序列化流；用于向流中写入对象的操作流 ObjectOutputStream 称为 序列化流</p>
    <p>特点：用于操作对象。可以将对象写入到文件中，也可以从文件中读取对象。</p>
    <h3>对象序列化流ObjectOutputStream</h3>
    <p>ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream。可以使用 ObjectInputStream 读取（重构）对象。通过在流中使用文件可以实现对象的持久存储。</p>
    <h4>只能将支持 java.io.Serializable 接口的对象写入流中</h4>
    <p>代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> io;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.FileOutputStream;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.ObjectOutputStream;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ObjectStreamDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException, ClassNotFoundException {
            </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
             * 将一个对象存储到持久化(硬盘)的设备上。
             </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
            writeObj();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对象的序列化。</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> writeObj() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1,明确存储对象的文件。</span>
            FileOutputStream fos = <span style="color: rgba(0, 0, 255, 1)">new</span> FileOutputStream("obj.object"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2，给操作文件对象加入写入对象功能。</span>
            ObjectOutputStream oos = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectOutputStream(fos);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3，调用了写入对象的方法。</span>
            oos.writeObject(<span style="color: rgba(0, 0, 255, 1)">new</span> Person("wangcai",20<span style="color: rgba(0, 0, 0, 1)">));
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关闭资源。</span>
    <span style="color: rgba(0, 0, 0, 1)">        oos.close();
        }
    }</span></pre>
    </div>
    <p>Person类</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> io;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.Serializable;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Person <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Serializable {
        </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String name;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age;
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Person() {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> Person(String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
        }
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getAge() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> age;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setAge(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
        }
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Person [name=" + name + ", age=" + age + "]"<span style="color: rgba(0, 0, 0, 1)">;
        }
    }</span></pre>
    </div>
    <h3>对象反序列化流ObjectInputStream</h3>
    <p>&nbsp;ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。支持 java.io.Serializable接口的对象才能从流读取。</p>
    <p>&nbsp;代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> io;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.FileInputStream;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.ObjectInputStream;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ObjectStreamDemo1 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException, ClassNotFoundException {
            readObj();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对象的反序列化。</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> readObj() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException, ClassNotFoundException {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1,定义流对象关联存储了对象文件。</span>
            FileInputStream fis = <span style="color: rgba(0, 0, 255, 1)">new</span> FileInputStream("obj.object"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2,建立用于读取对象的功能对象。</span>
            ObjectInputStream ois = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectInputStream(fis);
            Person obj </span>=<span style="color: rgba(0, 0, 0, 1)"> (Person)ois.readObject();
            System.out.println(obj.toString());
        }
    }</span></pre>
    </div>
    <h2>序列化接口&nbsp;</h2>
    <p>当一个对象要能被序列化，这个对象所属的类必须实现Serializable接口。否则会发生异常NotSerializableException异常。<br>同时当反序列化对象时，如果对象所属的class文件在序列化之后进行的修改，那么进行反序列化也会发生异常InvalidClassException。发生这个异常的原因如下：</p>
    <ul>
    <li>该类的序列版本号与从流中读取的类描述符的版本号不匹配 </li>
    <li>该类包含未知数据类型 </li>
    <li>该类没有可访问的无参数构造方法 </li>
    
    
    </ul>
    <p>Serializable标记接口。该接口给需要序列化的类，提供了一个序列版本号。serialVersionUID. 该版本号的目的在于验证序列化的对象和对应类是否版本匹配。</p>
    <p>代码修改如下，修改后再次写入对象，读取对象测试。&nbsp;</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Person <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Serializable {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给类显示声明一个序列版本号。</span>
        <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">long</span> serialVersionUID = 1L<span style="color: rgba(0, 0, 0, 1)">;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String name;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age;
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Person() {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
            
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> Person(String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
        }
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getAge() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> age;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setAge(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
        }
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Person [name=" + name + ", age=" + age + "]"<span style="color: rgba(0, 0, 0, 1)">;
        }
    }</span></pre>
    </div>
    <h2>瞬态关键字transient&nbsp;</h2>
    <p>当一个类的对象需要被序列化时，某些属性不需要被序列化，这时不需要序列化的属性可以使用关键字transient修饰。只要被transient修饰了，序列化时这个属性就不会被序列化了。<br>同时静态修饰也不会被序列化，因为序列化是把对象数据进行持久化存储，而静态的属于类加载时的数据，不会被序列化。<br>代码修改如下，修改后再次写入对象，读取对象测试。</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Person <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Serializable {
        </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
         * 给类显示声明一个序列版本号。
         </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">long</span> serialVersionUID = 1L<span style="color: rgba(0, 0, 0, 1)">;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> String name;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">transient</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">瞬态</span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age;
        
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Person() {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
            
        }
        
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> Person(String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
        }
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getAge() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> age;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setAge(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
        }
    
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Person [name=" + name + ", age=" + age + "]"<span style="color: rgba(0, 0, 0, 1)">;
        }
    }</span></pre>
    </div>
    <h2>打印流</h2>
    <p>打印流添加输出数据的功能，使它们能够方便地打印各种数据值表示形式.</p>
    <p>打印流根据流的分类：</p>
    <ul>
    <li>字节打印流 PrintStream</li>
    <li>字符打印流 PrintWriter</li>
    </ul>
    <p>方法：</p>
    <ul>
    <li>void print(String str): 输出任意类型的数据，</li>
    <li>void println(String str): 输出任意类型的数据，自动写入换行操作</li>
    </ul>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> io;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.PrintWriter;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    * 需求：把指定的数据，写入到printFile.txt文件中
    *
    * 分析：
    *     1，创建流
    *     2，写数据
    *     3，关闭流
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> PrintWriterDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建流
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"));</span>
            PrintWriter out = <span style="color: rgba(0, 0, 255, 1)">new</span> PrintWriter("printFile.txt"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2，写数据</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i=0; i&lt;5; i++<span style="color: rgba(0, 0, 0, 1)">) {
                out.println(</span>"helloWorld"<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3,关闭流</span>
    <span style="color: rgba(0, 0, 0, 1)">        out.close();
        }
    }</span></pre>
    </div>
    <h3>打印流完成数据自动刷新</h3>
    <p>可以通过构造方法，完成文件数据的自动刷新功能</p>
    <p>构造方法：开启文件自动刷新写入功能</p>
    <ul>
    <li>public PrintWriter(OutputStream out, boolean autoFlush)</li>
    <li>public PrintWriter(Writer out, boolean autoFlush)</li>
    </ul>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> io;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.FileWriter;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.PrintWriter;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
     * 分析：
     *     1，创建流
     *     2，写数据
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> PrintWriterDemo2 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建流</span>
            PrintWriter out = <span style="color: rgba(0, 0, 255, 1)">new</span> PrintWriter(<span style="color: rgba(0, 0, 255, 1)">new</span> FileWriter("printFile.txt"), <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2，写数据</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i=0; i&lt;5; i++<span style="color: rgba(0, 0, 0, 1)">) {
                out.println(</span>"helloWorld"<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3,关闭流</span>
    <span style="color: rgba(0, 0, 0, 1)">        out.close();
        }
    }</span></pre>
    </div>
    <h2>commons-IO</h2>
    <p>1、导入classpath</p>
    <ul>
    <li>加入classpath的第三方jar包内的class文件才能在项目中使用</li>
    <li>创建lib文件夹</li>
    <li>将commons-io.jar拷贝到lib文件夹</li>
    <li>导入jar包</li>
    </ul>
    <p>2、FilenameUtils<br>这个工具类是用来处理文件名（译者注：包含文件路径）的，他可以轻松解决不同操作系统文件名称规范不同的问题<br>常用方法：</p>
    <ul>
    <li>getExtension(String path)：获取文件的扩展名；</li>
    <li>getName()：获取文件名；</li>
    <li>isExtension(String fileName,String ext)：判断fileName是否是ext后缀名；</li>
    
    
    </ul>
    <p>3、FileUtils<br>提供文件操作（移动文件，读取文件，检查文件是否存在等等）的方法。<br>常用方法：</p>
    <ul>
    <li>readFileToString(File file)：读取文件内容，并返回一个String；</li>
    <li>writeStringToFile(File file，String content)：将内容content写入到file中；</li>
    <li>copyDirectoryToDirectory(File srcDir,File destDir);文件夹复制</li>
    <li>copyFile(File srcFile,File destFile);文件夹复制</li>
    
    
    </ul>
    <p>代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> io;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.io.FileUtils;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span> java.io.*<span style="color: rgba(0, 0, 0, 1)">;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
     * 完成文件的复制
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> CommonsIODemo01 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">method1("D:\\test.avi", "D:\\copy.avi");
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过Commons-IO完成了文件复制的功能</span>
            FileUtils.copyFile(<span style="color: rgba(0, 0, 255, 1)">new</span> File("D:\\test.avi"), <span style="color: rgba(0, 0, 255, 1)">new</span> File("D:\\copy.avi"<span style="color: rgba(0, 0, 0, 1)">));
    
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过Commons-IO完成了文件夹复制的功能
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">D:\java 复制到 C:\\abc文件夹下</span>
            FileUtils.copyDirectoryToDirectory(<span style="color: rgba(0, 0, 255, 1)">new</span> File("D:\\java"), <span style="color: rgba(0, 0, 255, 1)">new</span> File("C:\\abc"<span style="color: rgba(0, 0, 0, 1)">));
        }
    }</span></pre>
    </div>
    <p>&nbsp;</p>
    </div>
    
</body>
</html>